package com.ilink.groundservice.service.impl.Note;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ilink.groundservice.entity.PO.study.course.NoteVideo;
import com.ilink.groundservice.mapper.study.course.NoteVideoMapper;
import com.ilink.groundservice.service.Note.VideoNoteRecordService;
import com.ilink.ilinkcommon.domain.R;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class VideoNoteRecordServiceImpl extends ServiceImpl<NoteVideoMapper, NoteVideo>
        implements VideoNoteRecordService {

    @Autowired
    private NoteVideoMapper noteVideoMapper;

    @Override
    public R getNoteRecord(Long courseId) {
        log.info("getNoteRecord方法开始执行, 参数 courseId: {}", courseId);
        try {
            List<NoteVideo> notes;

            if (courseId == null) {
                log.info("courseId为空，查询所有记录");
                notes = noteVideoMapper.selectList(null);
            } else {
                log.info("根据courseId={}查询记录", courseId);
                notes = noteVideoMapper.selectList(new QueryWrapper<NoteVideo>().eq("course_id", courseId));
            }

            if (notes.isEmpty()) {
                log.warn("未找到对应courseId={}的记录", courseId);
                return R.ok("No records found for the given courseId.");
            }

            // 用于存储所有记录的结果
            List<Map<String, Object>> responseList = new ArrayList<>();

            for (NoteVideo note : notes) {
                log.info("处理NoteVideo记录，noteId={}, courseId={}, courseSourceId={}",
                        note.getNoteId(), note.getCourseId(), note.getCoursesourceId());

                // 获取名称
                String name1 = noteVideoMapper.getCourseNameByCourseId(note.getCourseId());
                String name2 = noteVideoMapper.getFileNameByCourseSourceId(note.getCoursesourceId());
                Long fileId1 = noteVideoMapper.getFileIdByCourseSourceId(note.getCoursesourceId());
                String name3 = noteVideoMapper.getNameByFileId(fileId1);
                String name4 = noteVideoMapper.getParentNameByFileId(fileId1);

                log.debug("获取的名称信息: name1={}, name2={}, name3={}, name4={}", name1, name2, name3, name4);

                // 拼接名称
                String finalName = name1 + name4 + name3 + name2;
                log.info("拼接后的finalName: {}", finalName);

                // 创建单条记录的 Map
                Map<String, Object> record = new HashMap<>();
                record.put("noteContent", note.getNoteContent());
                record.put("finalName", finalName);
                record.put("updateTime", note.getUpdatedAt());

                // 添加到结果列表
                responseList.add(record);
            }

            log.info("getNoteRecord方法执行完成，返回结果: {}", responseList);
            return R.ok("大屏查询视频笔记记录completed", responseList);
        } catch (Exception e) {
            log.error("函数getNoteRecord发生异常，courseId={}", courseId, e);
            return R.error("大屏查询视频笔记记录失败：" + e.getMessage());
        }
    }
}
